{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典（dict）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "95"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例：\n",
    "# 假设要根据同学的名字查找对应的成绩，如果用list实现，需要两个lis\n",
    "names = ['Michael', 'Bob', 'Tracy']\n",
    "scores = [95, 75, 85]\n",
    "# 用dict实现\n",
    "d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}\n",
    "d['Michael']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "67"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['Adam'] = 67\n",
    "d['Adam']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "88"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一个key只能对应一个value\n",
    "d['Jack'] = 90\n",
    "d['Jack']\n",
    "90\n",
    "d['Jack'] = 88\n",
    "d['Jack']\n",
    "88"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 如果key不存在，dict就会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'Thomas'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-bf27a9c462ee>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Thomas'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'Thomas'"
     ]
    }
   ],
   "source": [
    "d['Thomas']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 要避免key不存在的错误，有两种办法\n",
    "- 一是通过in判断key是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'Thomas' in d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   -  二是通过dict提供的get()方法，如果key不存在，可以返回None，或者自己指定的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.get('Thomas')\n",
    "d.get('Thomas', -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "75"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除一个key吗，用pop(key)方法，对应的value也会从dict中删除\n",
    "d.pop('Bob')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Michael': 95, 'Tracy': 85, 'Adam': 67, 'Jack': 88}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 和list比较，dict有以下几个特点：\n",
    "    - 查找和插入的速度极快，不会随着key的增加而变慢；\n",
    "    - 需要占用大量的内存，内存浪费多。\n",
    "- 而list相反：\n",
    "    - 查找和插入的时间随着元素的增加而增加；\n",
    "    - 占用空间小，浪费内存很少。\n",
    "- 所以，dict是用空间来换取时间的一种方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "正确使用dict非常重要，需要牢记的第一条就是dict的key必须是不可变对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合（list）\n",
    "set和dict类似，也是一组key的集合，但不存储value。由于key不能重复，所以，在set中，没有重复的key。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个set\n",
    "s = set([1, 2, 2, 3, 1])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 5}\n",
      "{1, 2, 3, 5}\n"
     ]
    }
   ],
   "source": [
    "# add()方法添加元素到set中\n",
    "s.add(5)\n",
    "print(s)\n",
    "s.add(2)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3, 5}\n"
     ]
    }
   ],
   "source": [
    "# remove()方法删除元素\n",
    "s.remove(1)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3}\n",
      "{1}\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 集合函数\n",
    "# intersection: 交集\n",
    "# difference:差集\n",
    "# union: 并集\n",
    "# issubset: 检查一个集合是否为另一个子集\n",
    "# issuperset: 检查一个集合是否为另一个超集\n",
    "s1 = {1, 2, 3}\n",
    "s2 = {2, 3, 4, 5}\n",
    "s_1 = s1.intersection(s2)\n",
    "print(s_1)\n",
    "\n",
    "s_2 = s1.difference(s2)\n",
    "print(s_2)\n",
    "\n",
    "s_3 = s1.issubset(s2)\n",
    "print(s_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
